-- NewsPaperRender21
-- 对应样式 2*1

local NewTeamMemberHeadRender = require("app.team.views.renders.NewTeamMemberHeadRender")
local UserBaseInfo = require("app.public.dao.UserBaseInfo")

local ClassRef = app.mvc.createRenderClass(...)

-- 不同行数的标题对应的不同图片（key=行数）
local LineToPic = {[1] = "P_Grid2x1_1T", [2] = "P_Grid2x1_2T"}
local LineToPicC = {[1] = "P_Grid2x1_1T_Character", [2] = "P_Grid2x1_2T_Character"}

function ClassRef.create(node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.NewsPaperRender_21))
    sprite:init()
    return sprite
end

function ClassRef:init(data)
    self.super.init(self, data)

    self._ui.text:setVisible(false)
    self._ui.title:setVisible(false)

    self._tltWidth, self._tltHeight = self._ui["Panel/text/tlt_1"]:getContentSize2()
    self._ui["Panel/text/tlt_1"]:setTextAreaSize(cc.size(self._tltWidth, 0))
    self._ui["Panel/title/tlt_1"]:setTextAreaSize(cc.size(self._tltWidth, 0))
    self._ui["Panel/text/tlt_1"]:getVirtualRenderer():setLineBreakWithoutSpace(true)    
    self._ui["Panel/title/tlt_1"]:getVirtualRenderer():setLineBreakWithoutSpace(true) 
    
    self._headIcon = NewTeamMemberHeadRender.create( self._ui["Panel/text/character"] )

    self:refresh()
end

function ClassRef:refresh()
    if self._data then
        local data = self._data
        local eventId = data.eventId
        local playerInfo = data.newsInfo or {}
        local extraStr = (playerInfo[1] and playerInfo[1].BaseInfo) and playerInfo[1].BaseInfo.name or "" -- 目前只需要玩家名字
        local conf = GD:getNewsPaperEventById(eventId)
        if not conf or not conf.ui21 then return end
        if conf.ui21 == "text" then
            self._ui.text:setVisible(true)
            self:initTextNode()
            local imageNode1 = self.imageNode11
            local imageNode2 = self.imageNode12
            local rowToPic = LineToPic
            local tltNode = self._ui["Panel/text/tlt_1"]
            local imageWidth = self.imageNode11c:getContentSize().width
            if tonumber(conf.player) == 1 then -- 有人物图片 (标题宽度缩短)
                self._tltWidth = imageWidth
                tltNode:setTextAreaSize(cc.size(self._tltWidth, 0))     
                imageNode1 = self.imageNode11c
                imageNode2 = self.imageNode12c
                rowToPic = LineToPicC
                if playerInfo[1] and playerInfo[1].BaseInfo then
                    local baseInfo = UserBaseInfo.new(playerInfo[1].BaseInfo)
                    self._headIcon:setData(baseInfo)
                    self._headIcon:setLvVisible(false)
                    self._headIcon:onClick(function(sender)
                        if baseInfo.uid ~= UD.player.uid then --点击别人的头像
                            local size = sender:getPanelSize()
                            local pos = sender:convertToWorldSpace(cc.p(size.width, size.height/2))
                            UIHandler:showHeadMenu({online=true, attr=baseInfo, pos=pos})
                        end
                    end)
                end
                self._ui["Panel/text/bg"]:setVisible(true)
            end                 
            local titleStr = conf.describe and "    "..conf.title1 or ""..conf.title1
            tltNode:setString(string.format(L(titleStr), extraStr)) 
            tltNode:getVirtualRenderer():setLineSpacing(-5)               
            local bgPath = string.format("%s%s.png",Res.newsPaperPath,conf.pic21)
            local tltVirtucal = tltNode:getVirtualRendererSize()
            tltNode:setContentSize(tltVirtucal)
            if tltVirtucal.height - self._tltHeight > self._tltHeight/2 then
                imageNode2:setVisible(true)
                bgPath = string.format("%s%s.png",Res.newsPaperPath,rowToPic[2])
                imageNode2:loadTexture(bgPath)
            else
                imageNode1:setVisible(true)
                bgPath = string.format("%s%s.png",Res.newsPaperPath,rowToPic[1])
                imageNode1:loadTexture(bgPath)
            end
            if conf.describe then
                self._ui.Button_Check:setVisible(true)
                self._ui.Button_Check:setTouchEnabled(true)
                self._ui.Button_Check:addTouchEventListener(function(sender, eventType)
                    if eventType == ccui.TouchEventType.ended then
                        executeFunc(self._onOperate, "openDetailsView", data)
                    end
                end)
            end
        elseif conf.ui21 == "title" then
            self._ui.title:setVisible(true)           
            self._ui["Panel/title/tlt_1"]:setString(string.format(L(conf.title1), extraStr))
            local tltVirtucal = self._ui["Panel/title/tlt_1"]:getVirtualRendererSize()
            self._ui["Panel/title/tlt_1"]:setContentSize(tltVirtucal)
            local isMultiLine = tltVirtucal.height - self._tltHeight > self._tltHeight/2
            local posX, posY = self._ui["Panel/title/tlt_2"]:getPosition()
            local tlt2Height = self._ui["Panel/title/tlt_2"]:getContentSize().height
            if conf.title2 then               
                self._ui["Panel/title/tlt_2"]:setString(string.format(L(conf.title2), extraStr))
                self._ui["Panel/title/tlt_2"]:setVisible(true)
            else
                self._ui["Panel/title/tlt_1"]:setPosition(cc.p(posX, posY))
                self._ui["Panel/title/tlt_2"]:setVisible(false)
            end
            local bgPath = string.format("%s%s.png",Res.newsPaperPath,conf.pic21)
            self._ui["Panel/title/bg"]:loadTexture(bgPath)
        end
    end
end

function ClassRef:initTextNode()
    self.imageNode11 = self._ui["Panel/text/Image_11"]
    self.imageNode12 = self._ui["Panel/text/Image_12"]
    self.imageNode11c = self._ui["Panel/text/Image_11c"]
    self.imageNode12c = self._ui["Panel/text/Image_12c"]
    self.imageNode11:setVisible(false)
    self.imageNode12:setVisible(false)
    self.imageNode11c:setVisible(false)
    self.imageNode12c:setVisible(false)
end

function ClassRef:onOperate(cb)
    self._onOperate = cb
end

return ClassRef